/*
 *    Document   : McKit
 *    Created on : 04/03/2013, 08:59:30
 *    Author     : Fiúza <rbfiuza@gmail.com>
 */

Ext.define('FC.controller.McKit',{
    extend: 'Ext.app.Controller'
    ,stores:['McKit']
    ,models:['McKit']
    ,views:['McKitList']
    
    ,init : function() {
        this.control({

            'mcKitList': {
                edit: this.editMcKit
                
            }
        });
    }

            
    ,editMcKit : function(editor, obj) {
        //check if record is dirty
        if(obj.record.dirty){
            //check if the record is valid   
            console.log(obj.record.validate());
            if(obj.record.validate().isValid()){
                //Make your Ajax request to sync data
                mode = (obj.record.get('editMcKitId') === "") ? 'insert': 'update';
                this.syncData(obj, mode);
            }
        }
    }
     
    //Sync data with the server 
    ,syncData : function(obj, mode) {
        Ext.Ajax.request({
            url: 'atualizaMcKit',
            params: {
                store_id: 1,
                action: mode,
                rowIndex: obj.rowIdx,
                recordInfo: Ext.encode(this.getMcKitStore().getAt(obj.rowIdx).data)
            },
            scope:this,
            //method to call when the request is successful
            success: this.onSaveSuccess,
            //method to call when the request is a failure
            failure: this.onSaveFailure
        });
    }     

    ,onSaveFailure: function(err){
        //Alert the user about communication error
        Ext.MessageBox.alert('Status', 'Error occured during update McKit');
    }
    
    ,onSaveSuccess: function(response,opts){
        //Remove dirty
        var ajaxResponse = Ext.decode(response.responseText);
        if(ajaxResponse.success){
            //if we are doing an insert then get the new customerId 
            //and update the store record
            if(opts.params.action === 'insert'){
                mcKitId = ajaxResponse.mcKitId;
                this.getMcKitStore().getAt(opts.params.rowIndex).set('McKitId',mcKitId);
            }

//            this.getContratacaoPessoalStore().getAt(opts.params.rowIndex).set('id',ajaxResponse.contratacaoPessoal.polos);
            this.getMcKitStore().getAt(opts.params.rowIndex).set('dscMcKit',ajaxResponse.kit.dscMcKit);
            this.getMcKitStore().getAt(opts.params.rowIndex).set('qntDisciplina',ajaxResponse.kit.qntDisciplina);
            this.getMcKitStore().getAt(opts.params.rowIndex).set('qntKit',ajaxResponse.kit.qntKit);
            this.getMcKitStore().getAt(opts.params.rowIndex).set('qntTotal',ajaxResponse.kit.qntTotal);
            this.getMcKitStore().getAt(opts.params.rowIndex).set('vlrUnit',ajaxResponse.kit.vlrUnit);
            this.getMcKitStore().getAt(opts.params.rowIndex).set('vlrFinal',ajaxResponse.kit.vlrFinal);
            this.getMcKitStore().getAt(opts.params.rowIndex).set('indicacoesParametros',ajaxResponse.kit.indicacoesParametros);
            this.getMcKitStore().getAt(opts.params.rowIndex).commit();
//            this.getContratacaoPessoalStore().save();
        }
        else {
            Ext.MessageBox.alert('Status', 'Error occured during update Contratacao McKit');
        }
    }
});
